from sc_engine.apps.web import poster_method

from sc_engine.apps import messages as msgs

def make_app(app_name, root_config, config):
    root_url = config['update_url']
    password = config['update_password']
    poster = poster_method(root_url + '/update/')

    app = WebSiteInterfaceApp(poster, password)

    return {
        msgs.LeagueDataAnnouncement: app.league_data_announcement,
        msgs.LeagueStageDataAnnouncement: app.league_stage_data_announcement,
        msgs.PlayerDataAnnouncement: app.player_data_announcement,
        msgs.TeamDataAnnouncement: app.team_data_announcement,
        msgs.MapDataAnnouncement: app.map_data_announcement,
        msgs.ProleagueMatchIdAnnouncement: app.proleague_match_id_announcement,
        msgs.ProleagueMatchDataAnnouncement: app.proleague_match_data_announcement,
        msgs.GameDataAnnouncement: app.game_data_announcement,
        msgs.CommentarySetDataAnnouncement: app.commentary_set_data_announcement,
    }

class WebSiteInterfaceApp(object):
    """ Communicate with the web site to update data.

    Fires off POST requests to the web server to update specific data.
    """
    def __init__(self, poster, password):
        self.poster = poster
        self.password = password

    def _post(self, action, data):
        passworded_data = [self.password, data]
        self.poster(action, passworded_data)

    def league_data_announcement(self, msg):
        self._post('league', msg.to_dict())

    def league_stage_data_announcement(self, msg):
        self._post('league_stage', msg.to_dict())

    def player_data_announcement(self, msg):
        self._post('player', msg.to_dict())

    def team_data_announcement(self, msg):
        self._post('team', msg.to_dict())

    def map_data_announcement(self, msg):
        self._post('map', msg.to_dict())

    def proleague_match_id_announcement(self, msg):
        self._post('proleague_match_id', msg.to_dict())

    def proleague_match_data_announcement(self, msg):
        self._post('proleague_match_data', msg.to_dict())

    def game_data_announcement(self, msg):
        self._post('game', msg.to_dict())

    def commentary_set_data_announcement(self, msg):
        self._post('commentary_set', msg.to_dict())

